Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

224
Vistas
Using a procedure to update Column 2 in a Table A, by checking if column 1 value is present in a column in Table B [PostgreSQL]

I have two tables as follows:

Table A: Name, ID, value

Table B: ID, title

TableA.value is dependent on whether TableA.ID is present in TableB.ID.

I am trying to create a procedure and trigger so that whenever TableB is modified, the procedure is triggered to check if TableA.ID is in TableB.ID and sets TableA.value to 10.

I am using the following code and getting an error:

CREATE PROCEDURE update1
LANGUAGE SQL
AS $$
UPDATE tablA as a
SET a.value = 10
WHERE a.ID EXISTS ( SELECT b.ID 
                            FROM tableB as b
                           WHERE a.ID = b.ID)
$$;

I am an SQL noob, and this is the first time I am trying to use a procedure.

UPDATE

I was able to create a procedure which does the job when I manually run it using CALL

However, it does not have a RETURNS TRIGGER block within it. Adding that returns the error Procedure cannot return triggers.

If I create a trigger as follows

CREATE TRIGGER b_trigger
AFTER UPDATE OR INSERT ON b.ID
FOR EACH STATEMENT
EXECUTE PROCEDURE update1();

However, this returns the following

ERROR: function columbia_deli.manager_discount must return type trigger

SQL state: 42P17

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Try this using plpgsql Language:

Trigger Function:

CREATE OR REPLACE FUNCTION update1()
  RETURNS trigger AS
$BODY$
 
begin
update "TableA" set value= 10 where id =new.id;

return NEW;
end;

$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;

Then create trigger on TableB for after update event

CREATE TRIGGER trig_tableb
  AFTER UPDATE
  ON "TableB"
  FOR EACH ROW
  EXECUTE PROCEDURE update1();
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda